package com.luckberry.murder.controller.api;

import com.alibaba.fastjson.JSON;
import com.luckberry.murder.entity.good.Order;
import com.luckberry.murder.service.OrderService;
import com.luckberry.murder.util.JSONUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping("/api")
public class ApiOrderController {

    @Resource
    private OrderService service;

    @ResponseBody
    @RequestMapping(value = "/orders", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getOrders() {
        return JSON.toJSONString(service.getOrders());
    }

    @ResponseBody
    @RequestMapping(value = "/order/{oid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getOrder(@PathVariable("oid") int oid) {
        return JSON.toJSONString(service.getOrder(oid));
    }

    @ResponseBody
    @RequestMapping(value = "/order", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public String addOrder(@RequestBody String json) {
        return JSONUtil.result(service.addOrder(JSON.parseObject(json, Order.class)));
    }

    @ResponseBody
    @RequestMapping(value = "/order/{oid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.DELETE)
    public String deleteOrder(@PathVariable("oid") int oid) {
        return JSONUtil.result(service.deleteOrder(oid));
    }

    @ResponseBody
    @RequestMapping(value = "/order", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.PUT)
    public String editOrder(@RequestBody String json) {
        return JSONUtil.result(service.editOrder(JSON.parseObject(json, Order.class)));
    }
}
